const mysql = require('mysql2/promise');
const dotenv = require('dotenv');

dotenv.config();

const pool = await mysql.createPool({
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0
});
async function query(sql, params) {
    const connection = await pool.getConnection();
    try {
        const [rows, fields] = await connection.execute(sql, params);
        return rows;
    } catch (error) {
        throw error;
    } finally {
        connection.release();
    }
}

module.exports = { query };